package com.mohe.shanpao.lucene;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class SearcherMain {

	public static void main(String[] args) throws IOException, ParseException {
		File indexDir = new File("D:\\data\\indexLucene");
		Directory dir = FSDirectory.open(Paths.get(indexDir.getPath()));
		
		IndexReader reader = DirectoryReader.open(dir);
		
		IndexSearcher searcher = new IndexSearcher(reader);  
		
		SmartChineseAnalyzer analyzer=new SmartChineseAnalyzer();
		QueryParser parser = new QueryParser("content", analyzer);
		Query query = parser.parse("轻视睡眠的后果");
		
//		Query query = new TermQuery(new Term("content","轻视睡眠的后果"));
		
		long start = System.currentTimeMillis();
		
		TopDocs topDocs = searcher.search(query, 10);
		
		long end = System.currentTimeMillis();
		
		for(ScoreDoc scoreDoc : topDocs.scoreDocs)
		{
			Document document = searcher.doc(scoreDoc.doc);
			System.out.println(document);
			System.out.println(document.get("path"));
		}
		
		System.out.println("匹配欧洲杯总共花费了"+(end-start)+"毫秒,共查到"+topDocs.totalHits+"条记录。");
		
		
		
		
	}

}
